package com.example.springbootlearn.util;

import com.example.springbootlearn.annotation.ORMColumn;
import com.example.springbootlearn.annotation.ORMId;
import com.example.springbootlearn.annotation.ORMTable;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 使用反射获取实体类中注解
 */
public class AnnotationUtil {
    /**
     * 获取类名
     */
    public static String getClass(Class clz){
        return clz.getName();
    }
    /**
     * 得到ORMTable注解中的表名
     */
    public static String getTableName(Class clz){
        if (clz.isAnnotationPresent(ORMTable.class)){
            ORMTable annotation = (ORMTable)clz.getAnnotation(ORMTable.class);
            return annotation.name();
        }else {
            System.out.println("缺少ORMTable注解");
            return null;
        }
    }
    /**
     * 得到主键属性和对应的字段
     */
    public static Map<String, String> getMapper(Class clz){
        boolean flag = true;
        HashMap<String, String> map = new HashMap<>();
        Field[] fields = clz.getDeclaredFields();
        for (Field field: fields) {
            if (field.isAnnotationPresent(ORMId.class)) {
                flag = false;
                String fieldName = field.getName();
                if (field.isAnnotationPresent(ORMColumn.class)){
                    ORMColumn ormColumn = field.getAnnotation(ORMColumn.class);
                    String columnName = ormColumn.name();
                    map.put(fieldName,columnName);
                    break;
                }else {
                    System.out.println(fieldName + "缺少ORMColumn注解");
                }
            }
        }
        if (flag) {
            System.out.println("缺少ORMId注解");
        }
        return map;
    }
    /**
     * 得到类中所有属性和对应的字段
     */
    public static Map<String, String> getPropMapping(Class clz){
        HashMap<String, String> map = new HashMap<>();
        map.putAll(getMapper(clz));
        Field[] fields = clz.getDeclaredFields();
        for (Field field: fields) {
            if (field.isAnnotationPresent(ORMColumn.class)) {
                ORMColumn ormColumn = field.getAnnotation(ORMColumn.class);
                String key = field.getName();
                String value = ormColumn.name();
                map.put(key, value);
            }
        }
        return map;
    }
    /**
     * 获得某包下面的所有类名
     */
    public static Set<String> getClassNameByPackage(String packagePath){
        HashSet<String> names = new HashSet<String>();
        String packfile = packagePath.replace(".","/");
        String classpath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        if (classpath == null) {
            classpath = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
        }
        try {
            classpath = URLEncoder.encode(classpath, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        File dir = new File(classpath + packfile);
        if (dir.exists()) {
            File[] files = dir.listFiles();
            for (File file: files){
                String fileName = file.getName();
                if (file.isFile() && fileName.endsWith(".class")) {
                    fileName = packagePath + "." + fileName.substring(0, fileName.lastIndexOf("."));
                    names.add(fileName);
                }
            }
        } else {
            System.out.println("包路径不存在");
        }
        return names;
    }
}
